﻿//==============================================================================
// File: ContextMenuItemProvider.cs
// Created: 2010-04-30
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using WpfRcp.Core.Common;
using Microsoft.Practices.ServiceLocation;

namespace WpfRcp.Core.Services.Commands
{
    public class ContextMenuItemProvider : IContextMenuItemProvider
    {
        private readonly IActionRegistry m_ActionRegistry;
        private readonly IMenuRegistry m_menuRegistry;
        private readonly IContextMenuItemFactory m_Factory;
        private readonly IServiceLocator m_ServiceLocator;

        public ContextMenuItemProvider(IActionRegistry actionRegistry, IMenuRegistry menuRegistry, IContextMenuItemFactory factory, IServiceLocator serviceLocator)
        {
            m_ActionRegistry = actionRegistry;
            m_menuRegistry = menuRegistry;
            m_Factory = factory;
            m_ServiceLocator = serviceLocator;
        }

        public virtual IEnumerable GetVisualItems(string name)
        {
            // Use default factory
            return GetVisualItems(name, m_Factory);
        }

        public virtual IEnumerable GetVisualItems(string name, string factoryName)
        {
            var factory = m_ServiceLocator.GetInstance<IContextMenuItemFactory>(factoryName);

            // Use given factory
            return GetVisualItems(name, factory);
        }

        private IEnumerable GetVisualItems(string name, IContextMenuItemFactory factory)
        {
            var items = m_menuRegistry.MenuItems.Where(candidateItem => candidateItem.Name == name).ToList();
            var list = new List<ItemsControl>();

            foreach (var item in items)
            {
                ICommandDescriptor command = null;
                string groupName = item.Group.Name;
                IMenuItemGroup group = m_menuRegistry.MenuItemGroups[groupName];

                if (item.Command != null && !string.IsNullOrEmpty(item.Command.Name))
                {
                    command = m_ActionRegistry.CommandDescriptors[item.Command.Name];
                }

                // Create the visual item.
                var visualItem = factory.GetVisualItem(item, group, command);
                list.Add(visualItem);

                GroupingViewsCollection.SetGroupIndex(visualItem, group.Position);
                GroupingViewsCollection.SetIndex(visualItem, item.Position);
            }

            return new GroupingViewsCollection(list, typeof(Separator));
        }
    }
}